package cn.hp.beauty.service.impl;

import cn.hp.beauty.domain.Menu;
import cn.hp.beauty.mapper.MenuMapper;
import cn.hp.beauty.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findMenuByRId(int rId) {
        return menuMapper.selectByRId(Long.valueOf(rId));
    }

    @Override
    public List<Menu> buildTreeMenu(List<Menu> oldMenuList) {
        List<Menu> resultMenuList = new ArrayList<>();

        for (Menu menu : oldMenuList) {
            for (Menu e : oldMenuList) {
                if (e.getMParentId() == menu.getMId()) {
                    menu.getChildren().add(e);
                }
            }

            if (menu.getMParentId() == 0L) {
                resultMenuList.add(menu);
            }
        }

        return resultMenuList;
    }
}
